//
//  CustomNavigatorView.swift
//  PhotoTestDemo
//
//  Created by 惠 on 2020/9/8.
//  Copyright © 2020 惠. All rights reserved.
//

import UIKit
import AVFoundation

class CustomNavigatorView: UIView {
    
    let screenW = UIScreen.main.bounds.width
    //是否开启闪光灯
    var isFlash: Bool = true
    //是否后置摄像头
    var isBackCamera: Bool = true
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    //MARK: -设置UI
    func setUI() {
        self.backgroundColor = .white
        
        addSubview(backButton)
        
        //相册内容
        addSubview(photoView)
        photoView.addSubview(selectPhotoButton)
//        photoView.addSubview(nextButton)
        
        //拍摄
        addSubview(videoView)
        videoView.addSubview(flashButton)
//        videoView.addSubview(scaleButton)
        videoView.addSubview(switchPhotoButton)
    }
    
//MARK: -懒加载UI
    //返回
    lazy var backButton: UIButton = {
        let backButton = UIButton(type: .custom)
        backButton.frame = CGRect(origin: CGPoint(x: 10, y: 5), size: CGSize(width: 35, height: 35))
        backButton.setImage(UIImage(named: "backButton", in: Resources.getBundle, compatibleWith: nil), for: .normal)
        return backButton
    }()
    
    //切换为相册内容
    lazy var photoView: UIView = {
        let photoView = UIView(frame: CGRect(origin: CGPoint(x: 50, y: 5), size: CGSize(width: screenW-50, height: 35)))
        photoView.backgroundColor = .clear
        return photoView
    }()
    lazy var selectPhotoButton: UIButton = {
        let selectPhotoButton = UIButton(type: .custom)
        selectPhotoButton.frame = CGRect(origin: CGPoint(x: (screenW-80)/2-50, y: 5), size: CGSize(width: 80, height: 24))
        selectPhotoButton.setTitle("最近项目", for: .normal)
        selectPhotoButton.setTitleColor(UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1), for: .normal)
        selectPhotoButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        selectPhotoButton.setImage(UIImage(named: "upArrow", in: Resources.getBundle, compatibleWith: nil), for: .normal)
        selectPhotoButton.setImage(UIImage(named: "downArrow", in: Resources.getBundle, compatibleWith: nil), for: .selected)
        //图右字左
        if #available(iOS 9.0, *) {
            selectPhotoButton.semanticContentAttribute = .forceRightToLeft
        }
        selectPhotoButton.layer.cornerRadius = 12
        selectPhotoButton.layer.masksToBounds = true
        selectPhotoButton.backgroundColor = UIColor(red: 0.97, green: 0.97, blue: 0.98, alpha: 1)
        selectPhotoButton.adjustsImageWhenHighlighted = false
        return selectPhotoButton
    }()
    
    lazy var nextButton: UIButton = {
        let nextButton = UIButton(type: .custom)
        nextButton.frame = CGRect(origin: CGPoint(x: screenW-125, y: 5), size: CGSize(width: 60, height: 24))
        nextButton.setTitle("完成", for: .normal)
        nextButton.setTitleColor(.white, for: .normal)
        nextButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        nextButton.backgroundColor = UIColor(red: 0.25, green: 0.76, blue: 0.8, alpha: 1)
        nextButton.layer.cornerRadius = 12
        nextButton.layer.masksToBounds = true
        return nextButton
    }()
    
    //切换为拍摄
    lazy var videoView: UIView = {
        let videoView = UIView(frame: CGRect(origin: CGPoint(x: 50, y: 5), size: CGSize(width: screenW-50, height: 35)))
        videoView.backgroundColor = .clear
        videoView.isHidden = true
        return videoView
    }()
    
    //闪光灯
    lazy var flashButton: UIButton = {
        let flashButton = UIButton(type: .custom)
        flashButton.frame = CGRect(origin: CGPoint(x: screenW-144, y: 5), size: CGSize(width: 19, height: 24))
        flashButton.setImage(UIImage(named: "flash", in: Resources.getBundle, compatibleWith: nil), for: .normal)
        flashButton.backgroundColor = .clear
        flashButton.addTarget(self, action: #selector(flashButtonAction(sender:)), for: .touchUpInside)
        return flashButton
    }()
    
    //比例
    lazy var scaleButton: UIButton = {
        let scaleButton = UIButton(type: .custom)
        scaleButton.frame = CGRect(origin: CGPoint(x: screenW-200, y: 5), size: CGSize(width: 24, height: 24))
        scaleButton.setImage(UIImage(named: "scale916", in: Resources.getBundle, compatibleWith: nil), for: .normal)
        scaleButton.backgroundColor = .clear
        return scaleButton
    }()
    
    //切换相机
    lazy var switchPhotoButton: UIButton = {
        let switchPhotoButton = UIButton(type: .custom)
        switchPhotoButton.frame = CGRect(origin: CGPoint(x: screenW-88, y: 7), size: CGSize(width: 23, height: 20))
        switchPhotoButton.setImage(UIImage(named: "switchPhoto", in: Resources.getBundle, compatibleWith: nil), for: .normal)
        switchPhotoButton.backgroundColor = .clear
        switchPhotoButton.addTarget(self, action: #selector(switchPhotoButtonAction(sender:)), for: .touchUpInside)
        return switchPhotoButton
    }()
    
    //切换底部模式调用
    func bottomClearColor(index: Int) {
        if index == 0 {
            backgroundColor = .white
            photoView.isHidden = false
            backButton.setImage(UIImage(named: "backButton", in: Resources.getBundle, compatibleWith: nil), for: .normal)
            videoView.isHidden = true
        } else if index == 1 {
            backgroundColor = .clear
            photoView.isHidden = true
            backButton.setImage(UIImage(named: "whiteBack", in: Resources.getBundle, compatibleWith: nil), for: .normal)
            videoView.isHidden = false
            flashButton.isHidden = false
        } else {
           backgroundColor = .clear
            photoView.isHidden = true
            backButton.setImage(UIImage(named: "whiteBack", in: Resources.getBundle, compatibleWith: nil), for: .normal)
            videoView.isHidden = false
            flashButton.isHidden = false
        }
    }
    
}

//MARK: -点击事件
extension CustomNavigatorView {
    //闪光灯
    @objc func flashButtonAction(sender: UIButton) {
        if isFlash {
            flashButton.setImage(UIImage(named: "flashed", in: Resources.getBundle, compatibleWith: nil), for: .normal)
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "navigatorAction"), object: self, userInfo: ["index": 1])
        } else {
            flashButton.setImage(UIImage(named: "flash", in: Resources.getBundle, compatibleWith: nil), for: .normal)
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "navigatorAction"), object: self, userInfo: ["index": 0])
        }
        isFlash = !isFlash
    }
    
    //切换摄像头
    @objc func switchPhotoButtonAction(sender: UIButton) {
        if isBackCamera {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "navigatorAction"), object: self, userInfo: ["camera": 1])
        } else {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "navigatorAction"), object: self, userInfo: ["camera": 0])
        }
        isBackCamera = !isBackCamera
    }

}
